package com.example.myapplication;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Sign.forgetPage;
import com.example.myapplication.Sign.loginPage;

public class personalPage extends AppCompatActivity {
    // 用户头像
    private ImageView avatar;
    // 用户名
    private TextView username;
    // 提示信息
    private TextView message;
    // 类别设置
    private TextView category;
    // 密码设置
    private TextView password;
    // 关于
    private TextView about;
    // 隐私协议
    private TextView agreement;
    // 退出登录
    private TextView logout;
    private ImageView main_bottom_01,
            main_bottom_02,
            main_bottom_03,
            main_bottom_04,
            main_bottom_05;

    //  登录状态保存变量
    SharedPreferences sf;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_page);

        avatar = findViewById(R.id.user_avatar);
        username = findViewById(R.id.user_username);
        message = findViewById(R.id.user_message);
        category = findViewById(R.id.change_category_btn);
        password = findViewById(R.id.change_password_btn);
        about = findViewById(R.id.user_about_btn);
        agreement = findViewById(R.id.user_agreement_btn);
        logout = findViewById(R.id.user_logout_btn);
        //        绑定底部导航栏
        main_bottom_01 = findViewById(R.id.main_bottom_01);
        main_bottom_02 = findViewById(R.id.main_bottom_02);
        main_bottom_03 = findViewById(R.id.main_bottom_03);
        main_bottom_04 = findViewById(R.id.main_bottom_04);
        main_bottom_05 = findViewById(R.id.main_bottom_05);

        //初始化状态保存器
        sf = getSharedPreferences("login_info", MODE_PRIVATE);
        editor = sf.edit();

        //显示信息
        showLogin(sf.getInt("login_state", 0));

//      导航底部绑定跳转事件
        main_bottom_01.setOnClickListener(view -> {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        });
        main_bottom_02.setOnClickListener(view -> {
            Intent intent = new Intent(this, ChartIncomeActivity.class);
            startActivity(intent);
        });
        main_bottom_03.setOnClickListener(view -> {
            Intent intent = new Intent(this, addFragment.class);
            startActivity(intent);
        });
        main_bottom_04.setOnClickListener(view -> {
            Intent intent = new Intent(this, FindActivity.class);
            startActivity(intent);
        });

        category.setOnClickListener(view -> {
            Intent intent = new Intent(personalPage.this, categorySettings.class);
            startActivity(intent);
        });


        //        密码设置
        password.setOnClickListener(view -> {
            Intent intent = new Intent(personalPage.this, forgetPage.class);
            startActivity(intent);
        });

        //  登录事件
        avatar.setOnClickListener(view -> {
            Intent intent = new Intent(personalPage.this, loginPage.class);
            startActivity(intent);
        });

//        开始时设为登录事件
        if (sf.getInt("login_state", 0) == 0){
            logout.setOnClickListener(view -> {
                Intent intent = new Intent(personalPage.this, loginPage.class);
                startActivity(intent);
            });
        }

//        登入后改为退出登录事件
        if (sf.getInt("login_state", 0) == 1) {
            logout.setText("退出登录");
            logout.setOnClickListener(view -> {
                editor.putInt("login_state", 0);
                editor.apply();
                showLogin(sf.getInt("login_state", 0));
            });
        }
    }

    private static void showLogin(int state){
        //  TODO: 显示我的页面上的信息 state为1是已登录，0是未登录
    }
}